/**
 *
 */
package gemini.basic.dao.impl;

import gemini.basic.dao.GmnDao;
import gemini.basic.dao.OrderStatusDao;
import gemini.basic.model.AbstractDomain;
import gemini.basic.model.OrderStatus;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 *
 *
 */
public class OrderStatusDaoImpl extends HibernateDaoSupport implements OrderStatusDao {

	private GmnDao gmnDao;

	public void setGmnDao(GmnDao gmnDao) {
		this.gmnDao = gmnDao;
	}

	@Override
	public List<OrderStatus> getAll() {

		DetachedCriteria dtCri = DetachedCriteria.forClass(OrderStatus.class);

		List<Object> orderList = gmnDao.findByCriteria(dtCri);
		List<OrderStatus> result = null;

		if (orderList != null) {
			result = new ArrayList<OrderStatus>(orderList.size());

			for (Object ob : orderList) {
				result.add((OrderStatus) ob);
			}
		}

		return result;
	}
	
	@Override
	public boolean deleteOrderStatus( OrderStatus orderStatus){
		getHibernateTemplate().delete(orderStatus);
		return true;
	}

	@Override
	public OrderStatus getById(Integer id) {
		DetachedCriteria dtCri = DetachedCriteria.forClass(OrderStatus.class);

		if (id != AbstractDomain.TRANSIENT_ENTITY_ID) {
			dtCri.add(Restrictions.eq("id", id));
		}

		List<Object> resultList = gmnDao.findByCriteria(dtCri);

		OrderStatus result = null;
		
		if (resultList != null && !resultList.isEmpty()) {
			result = (OrderStatus) resultList.get(0);
		}
		return result;
	}

	@Override
	public OrderStatus saveOrUpdate(OrderStatus ostatus, boolean flush) {
		OrderStatus result = null;
		if (flush) {
			result = (OrderStatus) gmnDao.saveOrUpdateAndFlush(ostatus);
		} else {
			result = (OrderStatus) gmnDao.saveOrUpdate(ostatus);
		}
		return result;
		
	}

}
